package com.backend.werewolfkillingbackend.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.io.IOException;
import java.util.List;

public class JsonUtil {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String parseListToJson(List list) throws JsonProcessingException {
        return objectMapper.writeValueAsString(list);
    }

    public static <T> List<T> parseJsonToList(String json, Class<T> elementType) throws IOException {
        TypeFactory typeFactory = objectMapper.getTypeFactory();
        JavaType listType = typeFactory.constructCollectionType(List.class, elementType);
        return objectMapper.readValue(json, listType);
    }

    public static <T> String parseObjToJson(T t) throws JsonProcessingException {
        return objectMapper.writeValueAsString(t);
    }

    public static <T> T parseJsonToObj(String json, Class<T> tClass) throws JsonProcessingException {
        return objectMapper.readValue(json, tClass);
    }
}
